<?php    
/**   
 * Aliyin oss vendor By lvwei   
 * 整理自 aliyun oss sdk 1.1.6
 * 时间 2015-06-05 15:38
 * 文件编码 utf-8   
 */
define('ALI_LOG_PATH',APPPATH.'logs/');
class Cls_oss{
    var $obj=null;
    var $bucket='';
    function __construct(){
        $this->CI =& get_instance();
        require_once APPPATH.'libraries/aliyun_oss/sdk.class.php';
        $this->obj = new ALIOSS();
    }
    
    //通过路径上传文件
    function upload_by_file($object,$file_path){
        $response = $this->obj->upload_file_by_file($this->bucket,$object,$file_path);
        if($response->status==200){
            $obj = $this->obj;
            $theheader=$response->header;
//             $read_url=str_replace($this->bucket.'.'.$obj::DEFAULT_OSS_HOST,$obj::READ_DOMAIN,$theheader['_info']['url']);
            $read_url=str_replace($obj::DEFAULT_OSS_HOST.'/'.$this->bucket,$obj::READ_DOMAIN,$theheader['_info']['url']);
            return array('obj_url'=>$read_url);
        }else{
            return false;
        }
    }
    
    //创建目录
    function create_directory($dir){
        //$bucket = 'phpsdk1349849394';
        //$dir = 'myfoloder-'.time();
        $response  = $this->obj->create_object_dir($this->bucket,$dir);
        if ($response->status==200){
            return true;
        } else {
            return false;
        }
        //$this->_format($response);
    }

    /** 按内容上传
    * @param $content 上传的内容
    * @param $object 显示的名称
    */
    function upload_by_content($content, $object){
        //$bucket = 'invalidxml3';
        //$folder = 'bbb/';
        
        $upload_file_options = array(
            'content' => $content,
            'length' => strlen($content),
        );
        //print_r($content);exit;
        $response = $this->obj->upload_file_by_content($this->bucket,$object,$upload_file_options);
        if( $response->isOk()  && $response->status==200){
            $obj = $this->obj;
            $theheader=$response->header;
            $read_url=str_replace($obj::DEFAULT_OSS_HOST.'/'.$this->bucket,$obj::READ_DOMAIN,$theheader['_info']['url']);
            return array('obj_url'=>$read_url);
        }
        else{
            return false;
        }
    }
    
    //格式化返回结果
    function _format($response) {
        echo '|-----------------------Start---------------------------------------------------------------------------------------------------'."\n";
        echo '|-Status:' . $response->status . "\n";
        echo '|-Body:' ."\n"; 
        echo $response->body . "\n";
        echo "|-Header:\n";
        print_r ( $response->header );
        echo '-----------------------End-----------------------------------------------------------------------------------------------------'."\n\n";
    }
}    